﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;
using CMS.Model;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using CMS.Model;
using System.Web.Script.Serialization;
using CMS.Logging;
namespace CMSWCSFSolution.CMS.Views
{
    public partial class CourseReport : BasePage, ICourseReportView
    {
        public static int RowCounts;
        private CourseReportPresenter _presenter;

        #region Protected Method

        /// <summary>
        /// Create new CourseReport Presenter
        /// </summary>
        [CreateNew]
        public CourseReportPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        /// <summary>
        /// Loads Page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                PageSize = 5;
                if (!this.IsPostBack)
                {
                    this._presenter.OnViewInitialized();
                    SelectAction();
                }
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.Highest);
                
            }
        }

        /// <summary>
        /// Select actions based on Client 
        /// </summary>
        public void SelectAction()
        {
            try
            {
                string action = string.Empty;
                if (!string.IsNullOrEmpty(Request.Params["action"]))
                {
                    action = Request.Params["action"].ToString();
                }
                switch (action)
                {
                    case "search":
                        {
                            GetCourses();
                        };
                        break;
                }
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.High);
                ShowMessage(3, "error");
            }
        }

        /// <summary>
        /// Get courses for json type
        /// </summary>
        public void GetCourses()
        {
            try
            {
                List<CourseList> courseList = _presenter.GetCoursesByCondition();
                string jsonString = new JavaScriptSerializer().Serialize(courseList).ToString();
                string returnData = string.Empty;
                returnData = "{";
                //total pages
                returnData += "\"totalpages\"";
                returnData += ":";
                returnData += "\"";
                returnData += PageCount;
                returnData += "\"";
                returnData += ",";

                //page index
                returnData += "\"page\"";
                returnData += ":";
                returnData += "\"";
                returnData += PageIndex;
                returnData += "\"";
                returnData += ",";


                //total records
                returnData += "\"totalrecords\"";
                returnData += ":";
                returnData += "\"";
                returnData += RowCount;
                returnData += "\"";
                returnData += ",";

                //add json data
                returnData += "\"griddata\"";
                returnData += ":";
                returnData += jsonString;

                returnData += "}";

                Response.Write(returnData);
                Response.End();
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.High);
                ShowMessage(3, "error");
            }
        }
        #endregion

        #region Public Property

        /// <summary>
        /// Teacher List for DropDownList
        /// </summary>
        public List<Teacher> TeacherList
        {
            set
            {
                this.TeacherNameDropDownList.DataSource = value;
                this.TeacherNameDropDownList.DataValueField = "TeacherId";
                this.TeacherNameDropDownList.DataTextField = "TeacherName";
                this.TeacherNameDropDownList.DataBind();
                this.TeacherNameDropDownList.Items.Insert(0, "--Choose Teacher--");
                this.TeacherNameDropDownList.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// Level List for DropDownList
        /// </summary>
        public List<Level> LevelList
        {
            set
            {
                this.LevelNameDropDownList.DataSource = value;
                this.LevelNameDropDownList.DataValueField = "LevelId";
                this.LevelNameDropDownList.DataTextField = "LevelName";
                this.LevelNameDropDownList.DataBind();
                this.LevelNameDropDownList.Items.Insert(0, "--Choose Level--");
                this.LevelNameDropDownList.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// CourseName From TextBox
        /// </summary>
        public string InputCourseName
        {
            get
            {
                if (string.IsNullOrEmpty(Request.Params["courseName"]))
                {
                    return string.Empty;
                }
                else
                {
                    return Request.Params["courseName"].ToString();
                }
            }
        }

        /// <summary>
        /// TeacherName From DropDownList
        /// </summary>
        public string InputTeacherName
        {
            get
            {
                var teacherName=string.Empty;
                if (!string.IsNullOrEmpty(Request.Params["teacherName"]))
                {
                    teacherName = Request.Params["teacherName"].ToString(); 
                }
                if (teacherName == "--Choose Teacher--")
                {
                    return string.Empty;
                }
                else return teacherName;
            }
            set { }
        }

        /// <summary>
        /// LevelName From DropDownList
        /// </summary>
        public string InputLevelName
        {
            get
            {
                var levelName = string.Empty;
                if (!string.IsNullOrEmpty(Request.Params["levelName"]))
                {
                    levelName = Request.Params["levelName"].ToString();
                }
                if (levelName == "--Choose Level--")
                {
                    return string.Empty;
                }
                else return levelName;
            }
            set { }
        }

        /// <summary>
        /// CourseStatus From CheckBox
        /// </summary>
        public int InputCourseStatus
        {
            get
            {
                if (Request.Params["open"] == "true" && Request.Params["closed"] == "false")
                {
                    return 1;
                }
                else if (Request.Params["open"] == "false" && Request.Params["closed"] == "true")
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }

        }

        /// <summary>
        /// OrderDirection From jgGrid
        /// </summary>
        public string OrderDirection
        {
            get
            {
                if (string.IsNullOrEmpty(Request.Params["sord"]))
                {
                    return "Desc";
                }
                else
                { 
                    return Request.Params["sord"].ToString();
                }
            }
        }

        /// <summary>
        /// SortExpression From jqGrid
        /// </summary>
        public string SortExpression
        {
            get
            {
                if (string.IsNullOrEmpty(Request.Params["sidx"]))
                {
                    return "CourseId";
                }
                else
                {
                    return Request.Params["sidx"].ToString();
                }
            }
        }

        /// <summary>
        /// PageSize to for Paging Function
        /// </summary>
        public int PageSize
        {
            get;
            set;
        }

        /// <summary>
        /// Int PageIndex to Show Current Page
        /// </summary>
        public int PageIndex
        {
            get
            {
                return Convert.ToInt32(Request.Params["page"]);
            }
        }

        /// <summary>
        /// PageCount to Show All Pages
        /// </summary>
        public int PageCount
        {
            get { return (RowCount + PageSize - 1) / PageSize; }
        }

        /// <summary>
        /// RowCount to show all Records
        /// </summary>
        public int RowCount
        {
            get
            {
                return RowCounts;
            }
            set
            {
                RowCounts = value;
            }
        }

        #endregion
    }
}

